/*
 * 
 * Copyright 2010 David H Pollack
 * 
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * 
 *     http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License. 
 * 
 * 
 */

package fantasy;

import java.io.IOException;

import javax.jdo.PersistenceManager;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.google.appengine.api.datastore.Key;
import com.google.appengine.api.datastore.KeyFactory;
import com.google.appengine.api.users.User;
import com.google.appengine.api.users.UserService;
import com.google.appengine.api.users.UserServiceFactory;
import com.google.gson.Gson;

/**
 * @author dpollack
 *
 */
public class GsonTestServlet extends HttpServlet {
	
	public void doGet(HttpServletRequest req, HttpServletResponse resp)
	throws ServletException, IOException {
		
		UserService userService = UserServiceFactory.getUserService();
        User user = userService.getCurrentUser();

        PersistenceManager pm = PMF.get().getPersistenceManager();
        
        Key key = new KeyFactory.Builder(LeagueSettings.class.getSimpleName(),"238.l.142523")
        						.addChild(LeagueTeams.class.getSimpleName(), "238.l.142523.t.1")
        						.addChild(LeagueStandings.class.getSimpleName(),"238.l.142523.t.1-1278146340").getKey();
        
        Key key2 = KeyFactory.createKey(LeagueStandings.class.getSimpleName(), "238.l.142523.t.1-1278146340");
        
        Key key3 = KeyFactory.createKey(LeagueStandings.class.getSimpleName(), "238");
        
        StringBuilder s = new StringBuilder();
        s.append("\n");
        s.append("Keys: ");
        s.append("\n");
        s.append(key);
        s.append("\n");
        s.append(key2 + "|" + key2.getId());
        s.append("\n");
        s.append(key3 + "|" + key3.getId());
        
        log(s.toString());
        
        LeagueStandings lx = pm.getObjectById(LeagueStandings.class,key);
        
        Gson gson = new Gson();
        
        String jsonObject = gson.toJson(lx);
        
        resp.getWriter().println("Json: " + jsonObject);
        
		
	}

}
